package com.lyb.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyb.Dto.BookDto;
import com.lyb.mapper.BookCategoryMapper;
import com.lyb.mapper.BookMapper;
import com.lyb.pojo.Book;
import com.lyb.pojo.BookCategory;
import com.lyb.services.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BookServiceImpl  implements BookService {
    @Autowired
    BookMapper bookMapper;
    @Autowired
    BookCategoryMapper bookCategoryMapper;
    @Override
    @Transactional
    public Boolean addBook(BookDto book) {
        bookMapper.addBook(book);
        Long id = book.getId();
        Long cateGoryId = book.getCategoryId();
        System.out.println("book = " + book);
        return bookCategoryMapper.add(id,cateGoryId);
    }

    @Override
    public PageInfo<Book> queryBookPage(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Book> res = bookMapper.queryBookPage();
        PageInfo pRes = new PageInfo<>(res);
        return pRes;
    }

    @Override
    public Boolean del(Integer id) {
        return bookMapper.del(id);
    }

    @Override
    public Boolean update(Book book) {
        return bookMapper.update(book);
    }

    @Override
    public BookCategory findByBookId(Long id) {
        return bookMapper.findByBookId(id);
    }
}
